<?php namespace Common\Model;
use Hdphp\Model\Model;

class Cate extends Model{
	//作用表名
	protected $table='category';
	//自动验证
	protected $validate=array(
		array('cname','required','分类名称不能为空',3,3)
	);
	//分类添加
	public function store(){
		if($this->create()){
			$this->add();
			return true;
		}
		return false;
	}
	//编辑
	public function edit(){
		if($this->create()){
			$this->save();
			return true;
		}
		return false;
	}
	//获得除了自己和自己的子集
	public function getNoMy($cid){
		//获得自己子集的cid
		$cids=$this->getSon($this->get(), $cid);
		//压入自己的cid
		$cids[]=$cid;
		//排除自己和自己的子集
		//SELECT * FROM category WHERE cid NOT IN(2,3,5);
		
		$where="cid NOT IN (".implode(',',$cids).")";
//		$where="cid NOT IN (".implode(',',$cids).")";
		
		return $this->where($where)->get();
	}
	//获取自己的子集
	public function getSon($data,$cid){
		$temp=array();
		//遍历数据
		foreach ($data as $k) {
			//如果有当前分类的子集
			if($k['pid']==$cid){
				//找到的子集压入临时数组
				$temp[]=$k['cid'];
				// 递归所有数组
				$temp=array_merge($temp,$this->getSon($data,$k['cid']));
			}
		}
		return $temp;
	}
	
}
?>